/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers.platformer {
	import com.puny.mvc.MvcEvent;
	import com.puny.mvc.controllers.keyscanner.KeyController;
	import com.puny.mvc.views.platformer.MapView;
	import com.puny.mvc.views.platformer.movieclips.MainCharacterView;
	import com.puny.mvc.views.platformer.movieclips.VerletCharacterView;
	import com.puny.utils.PetriNet;
	import com.puny.xmlutils.XMLReader;
	
	import flash.display.Sprite;
	import flash.events.Event;

	public class MapController extends EasingController {
		public static const MAP_READY:String = "map ready";
		private var _mainCharacter:MainCharacterView;
		protected var _map:MapView;
		private var _avatar:IAvatarView;
		private var _state:PetriNet;
		private var _keyScanner:KeyController;
		private var _mapURL:String;
		private var _mapName:String;
		private var _lockX:Boolean = false;
		private var _lockY:Boolean = false;
		
		public function MapController(url:String, name:String) {
			_mapURL = url;
			_mapName = name;
			super();
		}
		
		public function get mapURL():String {
			return _mapURL;
		}
		
		public function get mapName():String {
			return _mapName;
		}
		
		public function get lockX():Boolean {
			return _lockX;
		}
		
		public function set lockX(value:Boolean):void {
			_lockX = value;
		}
		
		public function get lockY():Boolean {
			return _lockY;
		}
		
		public function set lockY(value:Boolean):void {
			_lockY = value;
		}
		
		
		protected function get state():PetriNet {
			return _state;
		}
		
		protected function set state(value:PetriNet):void {
			_state = value;
		}
		
		protected function get keyScanner():KeyController {
			return _keyScanner;
		}
		
		protected function get mainCharacter():MainCharacterView {
			return _mainCharacter;
		}
		
		protected function get avatar():IAvatarView {
			return _avatar;
		}
		
		protected function set avatar(value:IAvatarView):void {
			_avatar = value;
		}

		protected function transit(value:String):Boolean {
//			trace("check", value, " and the current is", _state.currentState);
			var state:String = _state.transit(value);
			if (state != null && state!="") {
				trace("check", value, " and result is", state);
			}
			return state != null && state !="";
		}
		
		override protected function setup():void {
			_state.addEventListener(MvcEvent.READY, netReadyHandler);

			addChild(_avatar as Sprite);
			addChild(_map);
		}
		
		override public function beforeReady():void {
			super.beforeReady();
			
			_map.addEventListener(MvcEvent.COMPLETED, gameOverHandler);
			var key:XML = XMLReader.xmlDoc(PlatformerConfigure.CONFIGURE).key_operations[0];
			_keyScanner = new KeyController(key,stage);
			addKeyEventListeners();
			addChild(_keyScanner);

			_map.avatar = _avatar.mainCharacterView;
			_mainCharacter = _avatar.mainCharacterView;
			_mainCharacter.addEventListener(MainCharacterView.MOVING, slidingHandler);
			_mainCharacter.addEventListener(VerletCharacterView.OFF_GROUND, offGroundHandler);
			_mainCharacter.addEventListener(VerletCharacterView.ON_GROUND, onGroundHandler);
			slidingHandler();
		}
		
		protected function slidingHandler(event:Event=null):void {
			var dx:Number = lockX ? 0 : ((_mainCharacter.screenX-_mainCharacter.x) - x);
			var dy:Number = lockY ? 0 : ((_mainCharacter.screenY-_mainCharacter.y) - y);
			slideTo( dx , dy ,true);
		}
		
		override public function afterClose():void {
			super.afterClose();
			_mainCharacter.removeEventListener(MainCharacterView.MOVING, slidingHandler);
			_mainCharacter.removeEventListener(VerletCharacterView.OFF_GROUND, offGroundHandler);
			_mainCharacter.removeEventListener(VerletCharacterView.ON_GROUND, onGroundHandler);
			_state.close();
			removeKeyEventListeners();
			_keyScanner = null;
			_map = null;
		}
			
		protected function addKeyEventListeners():void {}
		
		protected function removeKeyEventListeners():void {}

		protected function gameOverHandler(event:Event):void {
			complete();
		}
		
		protected function offGroundHandler(event:Event):void {}
		protected function onGroundHandler(event:Event):void {}

		protected function netReadyHandler(event:Event):void {
			_state.removeEventListener(MvcEvent.READY, netReadyHandler);
		}
	}
}